<?php

/**
 * 访问标签类
 * Class AccessTagAction
 * @modify lilei<LL_strive@163.com> 2017-07-18
 */
class AccessTagAction extends Action {

    /**
     * 获得所有访问标签
     *
     * @param null $tagName     搜索某个标签
     * @param bool $needReturn  是否需要返回数组数据(内部接口调用)
     * @return mixed|void
     */
    public function getAccessTag($tagName = null, $mediaId = 0, $needReturn = false)
    {
        $retVal = Session::init();
        if (!$needReturn && !$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }

        $model = D('access_tag');
        $condition = '';
        $condition['mediaId'] = $mediaId;
        if ($tagName != '') {
            $condition['tagName'] = array("like", "%" . $tagName . "%");
        }
        $arr = $model->where($condition)->select();

        /** 返回 */
        if ($needReturn) {
            return $arr;
        }

        if ($arr) {
            $count = $model->count();
            Output::instance()->objArray($arr, $count);
        } else {
            Output::instance()->resultCode(code_success);
        }

    }

    public function getUserAccessTagList($fromUserName,$order="createTime", $orderBy="desc", $start = 0, $limit = 25,$tagName=null){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $customerId = Session::$user["customerId"];
        $model = D('user_access_tag');
        if(!empty($tagName)){
        $where["tagName"]=array("like","%$tagName%");
        }
        $where["accesstag.fromUserName"]=$fromUserName;
        $arr  = $model->alias('accesstag')->join("wx_".$customerId."_user user ON user.`userName` = accesstag.`fromUserName`")->join("wx_".$customerId."_user_weixin_info userinfo ON user.`userName` = userinfo.`openid`")->where($where)->order("$order $orderBy")->limit("$start, $limit")->select();
        //log::write("userAccessTagList sql:".$model->getLastSql());
        $count  = $model->alias('accesstag')->join("wx_".$customerId."_user user ON user.`userName` = accesstag.`fromUserName`")->join("wx_".$customerId."_user_weixin_info userinfo ON user.`userName` = userinfo.`openid`")->where($where)->count();

        if ($arr) {
            //$count = $model->count();
            Output::instance()->objArray($arr, $count);
        } else {
            Output::instance()->resultCode(code_success);
        }

    }

    /**
     * 添加标签
     *
     * @param $accessTags   支持多个添加, 用 "," 隔开
     * @param int $mediaId
     */
    public function addAccessTags($accessTags, $mediaId = 0)
    {
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }

        $accessTagModel = M('access_tag');
        $accessTags = explode(',', $accessTags);
        foreach ($accessTags as $key => $value) {
            $where['tagName'] = $value;
            $where['mediaId'] = $mediaId;
            //判断标签表是否存在标签 若不存在则新建标签
            $res = $accessTagModel->where($where)->find();
            $accesstagid = $res['id'];
            if (!isset($res)) {
                $result = $accessTagModel->add($where);
            }
        }

        if ($result) {
            $data["id"] = $result;
            Output::instance()->object($data);
        } else {
            Output::instance()->resultCode(code_sql_error);
        }
    }

    /**
     * 删除标签
     *
     * @param $id   标签id
     */
    public function delAccessTag($id)
    {
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }

        $AccessTagModel = M('access_tag');
        $where['id'] = $id;
        $res = $AccessTagModel->where($where)->delete();

        if ($res) {
            Output::instance()->resultCode(code_success);
        } else {
            Output::instance()->resultCode(code_sql_error);
        }
    }

}